//
//  HYCCollectController.m
//  FamilyTravel
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "HYCCollectController.h"
#import "HYCSecondPushDisModel.h"
#import "HYCHeadDocument.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface HYCCollectController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain) UITableView *tableView;
@end

@implementation HYCCollectController
- (void)dealloc {
    [_tableView release];
    
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我的收藏";

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    //编辑按钮
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    [self.tableView setEditing:YES animated:YES];
    
}



//重写系统的编辑按钮初始化方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
// 设置哪些可以编辑默认都可以
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 修改cell的编辑模式,默认是删除,还可以进行添加等操作
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete && indexPath.section == 1) {
        
        
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCLv.plist"];
        
        self.arr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

        [self.arr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [NSKeyedArchiver archiveRootObject:self.arr toFile:docPath];


    }else {
        
        NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
        NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"HYCCity.plist"];
        self.secArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
        
        [self.secArr removeObjectAtIndex:indexPath.row];
        
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [NSKeyedArchiver archiveRootObject:self.secArr toFile:docPath];

    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return self.arr.count;
    }else{
        return self.secArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *aReuse = @"aReuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:aReuse];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aReuse];
        }
        for (HYCSecondPushDisModel *tempModel in self.secArr) {
            NSLog(@"%@", tempModel.cnname);
        }
        cell.textLabel.text = [self.secArr[indexPath.row] cnname];
        return cell;
    } else {
        static NSString *reuse = @"reuse";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
        }
        cell.textLabel.text = [self.arr[indexPath.row] name];
        return  cell;
    }
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
//     if (self.arr.count == 0 && self.secArr.count == 0) {
    if (section == 0) {
        return @"城市";
    }else {
        return @"驴友分享";
    }
//   }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        
        HYCTourPalPushtroller *pRVC = [[HYCTourPalPushtroller alloc] init];
        pRVC.hidesBottomBarWhenPushed = YES;
            
        pRVC.str = [self.arr[indexPath.row] number];
        [self.navigationController pushViewController:pRVC animated:YES];
        pRVC.title = [self.arr[indexPath.row] name];
        pRVC.model = self.arr[indexPath.row];
        
    }else if(indexPath.section == 0){
        HYCPushCitytroller *pCVC = [[HYCPushCitytroller alloc] init];
        [self.navigationController pushViewController:pCVC animated:YES];
        
        pCVC.title = [self.secArr[indexPath.row] cnname];
        pCVC.model = self.secArr[indexPath.row];

        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
